package com.sys.orders.head.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.ABlindbox;
import com.sys.goods.pojo.ABlindboxEnum;
import com.sys.goods.pojo.AGoods;
import com.sys.goods.pojo.AGoodsEnum;
import com.sys.goods.service.IGoodsDbService;

@Controller
@RequestMapping("/")
public class IndexHeadController extends BaseController
{
	@Autowired
	private IGoodsDbService goodsDbService ;
	
	
	/**
	 * 首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/main")
	public String main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", AGoodsEnum.STATUS_ENABLE.getStatus());
		/* 按照销量排名的前20件商品 */
		request.setAttribute("orderby", "saleNumDesc");
		
		/* 扩展信息 */
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findCondListABlindboxService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		/* 页面跳转 */
		return "/head/main" ;
	}
	
	/**
	 * header
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/header")
	public String header(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 查询所有的分类 */
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findCondListABlindboxService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("headerResponse", apiResponse.toJSON());
		
		return "/head/header" ;
	}
}
